package com.itheima.autoconfig;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

//阿里存储工具类
@Component
@ConfigurationProperties(prefix = "tanhua.oss")//读取配置信息
@Data
public class OssTemplate {
    private String accessKey; //访问key
    private String secret; //访问密钥
    private String endpoint;
    private String bucketName;//桶名
    private String url;

    //文件上传
    public String upload(String fileName,InputStream inputStream){
        //创建客户端
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKey, secret);
        // 设置文件最终的路径和名称
        String objectName="images/"+new SimpleDateFormat("yyyy/MM/dd").format(new Date())
                +"/."+System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));
        //meta设置请求头,结局访问图片地址直接下载
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentType(getContentType(fileName.substring(fileName.lastIndexOf("."))));
        //上传
        ossClient.putObject(bucketName,objectName,inputStream,meta);
        //关闭客户端
        ossClient.shutdown();
        return url+"/"+objectName;
    }

    //文件后缀处理
    private String getContentType(String FilenameExtension) {
        if(FilenameExtension.equalsIgnoreCase(".bmp")){
            return "image/bmp";
        }
        if(FilenameExtension.equalsIgnoreCase(".jpeg") ||
        FilenameExtension.equalsIgnoreCase(".jpg") ||
        FilenameExtension.equalsIgnoreCase(".png")){
            return "image/jpg";
        }
        return "image/jpg";
    }

}